
package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.MaterialOutStockTaskAdapter;
import com.mflex.WHSmartWhouseAd.model.MaterialDetailForRawModel;
import com.mflex.WHSmartWhouseAd.model.MaterialTaskSubmitModel;
import com.mflex.WHSmartWhouseAd.model.RequestModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailInfoForRawModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailForRawModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailInfoForSMTModel;
import com.mflex.WHSmartWhouseAd.model.TaskSubimtForRawModel;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;


public class MaterialOutStockTaskActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    EditText stTaskNo;
    EditText stEmployee;
    EditText stVehicleNO;
    Spinner StvPrintID;
    EditText stvLabelQty;
    ListView txtList;
    MiniLoadingDialog mMiniLoadingDialog;
    Button buttonSubmit;
    String userName;
    String Op;
    String Floor;
    String SubTaskno;
    List<TaskDetailForRawModel> taskDetailListForRaw;
    TaskDetailInfoForRawModel TaskDetailInfoForRawModel;
    List<MaterialDetailForRawModel> MaterialDetailForRawList;

    String MaterialClass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_material_outstock_task);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        stTaskNo= findViewById(R.id.stvTaskNo);
        stEmployee= findViewById(R.id.stvEmployee);
        stVehicleNO= findViewById(R.id.stvVehicleNO);
        txtList= findViewById(R.id.lvList);
        StvPrintID= findViewById(R.id.StvPrintID);

        stvLabelQty= findViewById(R.id.stvLabelQty);
        buttonSubmit= findViewById(R.id.button_save);
        Intent intent = getIntent();

        userName = intent.getStringExtra("userName");
        Floor= intent.getStringExtra("Floor");
        MaterialClass= intent.getStringExtra("MaterialClass");

        SubTaskno=intent.getStringExtra("TaskNo");
        stTaskNo.setText(SubTaskno);
        stvLabelQty.setText("1");

//        Type listType = new TypeToken<List<SubTaskInfoModel>>() {
//        }.getType();
//        SubTaskInfoModelList = (List<SubTaskInfoModel>) JsonUtil.fromJson(intent.getStringExtra("SubTaskInfolist"), listType);
//        MaterialOutStockTaskAdapter MaterialOutStockTaskDetailAdapter = new MaterialOutStockTaskAdapter(MaterialOutStockTaskActivity.this, SubTaskInfoModelList);
//        txtList.setAdapter(MaterialOutStockTaskDetailAdapter);

        inittaskdetail(SubTaskno);
        mMiniLoadingDialog.dismiss();
        XHttp.get("api/WHSmartWhousePDA/GetPrintNameList")
                .syncRequest(false)
                .onMainThread(true)
                .execute(new SimpleCallBack<List<String>>() {
                    @Override
                    public void onSuccess(List<String> response) {
                        List<String> list = new ArrayList<>();
                        list.addAll(response);
                        String[] arr = list.toArray(new String[0]);
                        WidgetUtils.initSpinnerStyle(StvPrintID, arr);
                        StvPrintID.setSelection(0);
                    }
                    @Override
                    public void onError(ApiException e) {
                        MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(MaterialOutStockTaskActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });


        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mMiniLoadingDialog.show();
                TaskSubimtForRawModel model=new TaskSubimtForRawModel(stTaskNo.getText().toString(),StvPrintID.getSelectedItem().toString(),stvLabelQty.getText().toString(),stEmployee.getText().toString());
                RequestModel requestModel = new RequestModel();
                requestModel.setJson(JsonUtil.toJson(model));
                String json = JsonUtil.toJson(requestModel);
                System.out.println(json);
                XHttp.post("api/WHSmartWhousePDA/SmartTaskSubmitForRaw")
                        .upJson(JsonUtil.toJson(requestModel))
                        .syncRequest(false)
                        .onMainThread(true)
                        .timeOut(150000)
                        .execute(new SimpleCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean response) {
                                mMiniLoadingDialog.dismiss();
                                XToastUtils.success("打印成功",3000);

                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(MaterialOutStockTaskActivity.this)
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(e.getMessage())
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });

            }
        });
        stVehicleNO.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",stEmployee.getText().toString()))  {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描载具", 1500);
                        stEmployee.requestFocus();
                        return false;
                    }

                    XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailInfoForRawByVehicle")
                            .params("TaskNo",stTaskNo.getText().toString() )
                            .params("VehicleNumber", stVehicleNO.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<TaskDetailInfoForRawModel>() {
                                @Override
                                public void onSuccess(TaskDetailInfoForRawModel response) {

                                    mMiniLoadingDialog.dismiss();
                                    MaterialDetailForRawList=response.getMaterialDetailForRawList();
                                    TaskDetailInfoForRawModel=response;
                                    String MaterialPart="";
                                    String UnremovedQty="";
                                    String RemovedQty="";

                                    Boolean flag=false;
                                    for (TaskDetailForRawModel detail : taskDetailListForRaw) {
                                        if (StringUtils.equals(response.getJDEPN(), detail.getMaterialPart())) {
                                            MaterialPart=detail.getMaterialPart();
                                            UnremovedQty=detail.getUnremovedQty();
                                            RemovedQty=detail.getRemovedQty();
                                            flag = true;
                                        }
                                    }
                                    if(flag)
                                    {

                                        Intent intent = new Intent(MaterialOutStockTaskActivity.this, MaterialOutStockTaskDetailActivity.class);
                                        intent.putExtra("TaskNo", TaskDetailInfoForRawModel.getTaskNo().toString());
                                        intent.putExtra("Floor", TaskDetailInfoForRawModel.getFloor().toString());
                                        intent.putExtra("MaterialClass", TaskDetailInfoForRawModel.getMaterialClass().toString());
                                        intent.putExtra("JDEPN", TaskDetailInfoForRawModel.getJDEPN());
                                        intent.putExtra("UnmovedQty", UnremovedQty);
                                        intent.putExtra("movedQty", RemovedQty);
                                        intent.putExtra("userName", userName);

                                        intent.putExtra("TaskDetailID", TaskDetailInfoForRawModel.getTaskDetailID().toString());
                                        intent.putExtra("list", JsonUtil.toJson(MaterialDetailForRawList));
                                        intent.putExtra("OPT", stEmployee.getText().toString());
                                        startActivityForResult(intent,0);
                                    }
                                    else
                                    {
                                        new MaterialDialog.Builder(MaterialOutStockTaskActivity.this)
                                                .iconRes(R.drawable.icon_warning)
                                                .title(R.string.tip_warning)
                                                .content("列表中不存在该载具上的料")
                                                .positiveText(R.string.lab_submit)
                                                .show();
                                        return;
                                    }
                                    stVehicleNO.setText("");
                                    stVehicleNO.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    stVehicleNO.setText("");
                                    stVehicleNO.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });
        txtList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if(StringUtils.equals("",stEmployee.getText().toString()))  {
                    mMiniLoadingDialog.dismiss();
                    MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                    XToastUtils.success("请先扫描员工号", 1500);
                    stEmployee.requestFocus();
                    return;
                }
                String MaterialPart=taskDetailListForRaw.get(i).getMaterialPart().toString();

                XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailInfoForRawByVehicle")
                        .params("TaskNo", stTaskNo.getText().toString())
                        .params("MaterialPart", MaterialPart)
                        .syncRequest(false)
                        .onMainThread(true)
                        .timeOut(120000)
                        .execute(new SimpleCallBack<TaskDetailInfoForRawModel>() {
                            @Override
                            public void onSuccess(TaskDetailInfoForRawModel response) {

                                mMiniLoadingDialog.dismiss();
                                MaterialDetailForRawList=response.getMaterialDetailForRawList();
                                TaskDetailInfoForRawModel=response;

                                String UnremovedQty=taskDetailListForRaw.get(i).getUnremovedQty().toString();
                                String RemovedQty=taskDetailListForRaw.get(i).getRemovedQty().toString();

                                Intent intent = new Intent(MaterialOutStockTaskActivity.this, MaterialOutStockTaskDetailActivity.class);
                                intent.putExtra("TaskNo", TaskDetailInfoForRawModel.getTaskNo().toString());
                                intent.putExtra("Floor", TaskDetailInfoForRawModel.getFloor().toString());
                                intent.putExtra("MaterialClass", TaskDetailInfoForRawModel.getMaterialClass().toString());
                                intent.putExtra("JDEPN", TaskDetailInfoForRawModel.getJDEPN());
                                intent.putExtra("UnmovedQty", UnremovedQty);
                                intent.putExtra("movedQty", RemovedQty);
                                intent.putExtra("userName", userName);

                                intent.putExtra("TaskDetailID", TaskDetailInfoForRawModel.getTaskDetailID().toString());
                                intent.putExtra("list", JsonUtil.toJson(MaterialDetailForRawList));
                                intent.putExtra("OPT", stEmployee.getText().toString());
                                startActivityForResult(intent,0);
                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                stVehicleNO.setText("");
                                stVehicleNO.requestFocus();
                                MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(view.getContext())
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(e.getMessage())
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });
            }
        });
        stEmployee.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",stEmployee.getText().toString())) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);
                        mp.start();
                        XToastUtils.success("请扫描作业人员", 1500);
                        Op="";
                        stEmployee.requestFocus();
                        return false;

                    }
                    XHttp.get("api/WHSmartWhousePDA/CheckOperatorForRaw")
                            .params("Operator", stEmployee.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<Object>() {
                                @Override
                                public void onSuccess(Object response) {
                                    mMiniLoadingDialog.dismiss();
                                    Op=stEmployee.getText().toString();
                                    stVehicleNO.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    stEmployee.setText("");
                                    stEmployee.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(MaterialOutStockTaskActivity.this, R.raw.error_voice);
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                mMiniLoadingDialog.dismiss();
                return false;
            }


        });

    }

    private void inittaskdetail(String TaskNo)
    {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailListForRaw")
                .params("TaskNo", TaskNo)
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<List<TaskDetailForRawModel>>() {
                    @Override
                    public void onSuccess(List<TaskDetailForRawModel> response) {
                        mMiniLoadingDialog.dismiss();
                        taskDetailListForRaw=response;
                        MaterialOutStockTaskAdapter MaterialOutStockTaskDetailAdapter = new MaterialOutStockTaskAdapter(MaterialOutStockTaskActivity.this, taskDetailListForRaw);
                        txtList.setAdapter(MaterialOutStockTaskDetailAdapter);
                        stEmployee.setText("");
                        stEmployee.requestFocus();
                    }

                    @Override
                    public void onError(ApiException e) {
                        stEmployee.setText("");
                        stEmployee.requestFocus();
                        mMiniLoadingDialog.dismiss();
                        new MaterialDialog.Builder(MaterialOutStockTaskActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}



